<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('在线考试')"/>
</head>
<body class="gray-bg">


<div class="col-sm-12 search-collapse">
    <form id="formId">
        <div class="select-list">
            <ul>
                <li>
                    <label>名称：</label>
                    <input type="text" name="foodName"/>
                </li>
                <li>
                    <label>分类：</label>
                    <select name="foodType" th:with="type=${@dict.getType('food_type')}">
                        <option value="">所有</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                th:value="${dict.dictValue}"></option>
                    </select>
                </li>
                <li>
                    <label>价格：</label>
                    <input type="text" name="foodPrice"/>
                </li>
                <li>
                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                            class="fa fa-search"></i>&nbsp;搜索</a>
                    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                            class="fa fa-refresh"></i>&nbsp;重置</a>
                    <a href="/dc/orderDetail" class="btn btn-info btn-rounded btn-sm">我的购物车</a>
                </li>
            </ul>
        </div>
    </form>
</div>

<div class="container-div">
    <div class="row">
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-page-size="10"
                   data-show-custom-view="true" data-custom-view="customViewFormatter"
                   data-show-custom-view-button="true">
            </table>
        </div>
    </div>
</div>


<template id="profileTemplate">
    <div class="col-sm-6">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>%foodName%</h5>
            </div>
            <div class="ibox-content">
                <div class="row">
                    <div class="col-sm-4 b-r">
                        <p class="text-center">
                            <a href="#">
                                <img src="%foodImg%" style="width: 180px;height: 114px">
                            </a>
                        </p>
                    </div>
                    <div class="col-sm-8">
                        <a href="#" onclick="openHt('%id%')">
                            <h3 class="m-t-none m-b">%foodName%</h3>
                            <p>价格: %foodPrice% 元/份</p>
                            介绍：%describesStr%
                        </a>
                        <form id="form%id%">
                            <div class="col-sm-10">
                                <div class="row">
                                    <div class="col-md-2">
                                        <input type="text" value="数量" readonly
                                               style="margin-left: -19px;width: 45px;" class="form-control">
                                    </div>
                                    <div class="col-md-4">
                                        <input type="hidden" name="foodId" value="%id%">
                                        <input type="number" class="form-control" name="foodNumber" value="1"
                                               style="margin-left: -40px;">
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-12 col-sm-offset-3">
                                <button onclick="addCar('%id%')" type="button" class="btn btn-warning">加入购物车</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<div th:include="include :: footer"></div>
<th:block th:include="include :: bootstrap-table-custom-view-js"/>
<script th:inline="javascript">

    var foodTypeDatas = [[${@dict.getType('food_type')}]];
    var prefix = ctx + "dc/fodoInfo";
    var datas = [[${@dict.getType('sys_normal_disable')}]];

    $(function () {
        var options = {
            url: prefix + "/list",
            showSearch: false,
            showRefresh: true,
            showToggle: false,
            pageSize: 8,
            showColumns: false,
            showExport: true,
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '编号',
                    visible: false
                },
                {
                    field: 'foodName',
                    title: '名称'
                },
                {
                    field: 'foodContent',
                    title: '编号',
                    visible: false
                },
                {
                    field: 'foodType',
                    title: '分类',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(foodTypeDatas, value);
                    }
                },
                {
                    field: 'foodImg',
                    title: '图片',
                    formatter: function (value, row, index) {
                        return $.table.imageView(value, 600, 1200);
                    }
                },
                {
                    field: 'foodPrice',
                    title: '价格'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function customViewFormatter(data) {
        var template = $('#profileTemplate').html()
        var view = ''
        $.each(data, function (i, row) {
            var len = $.common.getLength(row.foodContent);
            var describesStr = "";
            if (Number(len) > 28) {
                describesStr = row.foodContent.slice(0, 13 - 1) + "......";
            } else {
                describesStr = row.foodContent;
            }

            view += template.replaceAll('%foodName%', row.foodName)
                .replaceAll('%id%', row.id)
                .replaceAll('%foodPrice%', row.foodPrice)
                .replaceAll('%foodType%', row.foodType)
                .replaceAll('%describesStr%', describesStr)
                .replaceAll('%foodImg%', row.foodImg)
                .replaceAll('%foodContent%', row.foodContent);
        })

        return `<div class="row mx-0">${view}</div>`
    }

    function openHt(id) {
        return;
        $.modal.open('添加用户', ctx + "demo/modal/form");
    }

    function addCar(id) {
        $.ajax({
            type: "post",
            url: ctx + "dc/orderDetail/add",
            data: $("#form" + id).serialize(),
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("添加成功");
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }


</script>
</body>
</html>